/**
 * ---------------------------
 * Time: 2017/5/14 12:40.
 * Author: Cangshi
 * View: http://palerock.cn
 * ---------------------------
 */
var ctx = c.ctx;

var RectEffect = Main.createConfigClass({
    default: {
        defaultVar: function () {
            return {
                // 初始成员变量
                InnerRect: Main.createConfigClass({
                    default: {
                        defaultConfig: function () {
                            return {
                                width: 1,
                                height: 1,
                                color: {type: 'random'},
                                runningTrack: {
                                    centrePoint: {
                                        x: 0,
                                        y: 0
                                    },
                                    speed: 1,
                                    direction: 135 + Math.random() * 90
                                }
                            }
                        },
                        onConfigured: function () {
                            this.centrePoint = new Cielar.Point(this.config.centrePoint)
                            this.height = this.config.height;
                            this.width = this.config.width;
                            this.color = new Cielar.Color(this.config.color);
                            this.runningTrack = new RunningTrack(this.config.runningTrack);
                        }
                    },
                    defaultPrototype: {
                        doGoing: function () {
                            this.color.a = this.color.a * 0.999 - 0.01;
                            if (this.color.a <= 0.05) {
                                this._onDeath();
                            }
                        },
                        _onDeath: function () {
                            this.stop();
                            this.isDestory = true;
                            this.onDeath();
                        },
                        onDeath: function () {

                        },
                        start: function () {
                            var _this = this;
                            this.runningTrack.doChanges = function () {
                                _this.doGoing();
                            };
                            this.runningTrack.runningStart();
                        },
                        stop: function () {
                            this.runningTrack.runningEnd();
                        }
                    }
                })
            }
        },
        defaultConfig: function () {
            return {
                maxRect: 200,
                width: 1,
                height: 1,
                color: {type: 'random'},
                runningTrack: {
                    centrePoint: {
                        x: 150,
                        y: 500
                    },
                    speed: 2,
                    acceleration: 0.05,
                    direction: 0
                }
            }
        },
        onConfigured: function () {
            this.centrePoint = new Cielar.Point(this.config.centrePoint);
            this.height = this.config.height;
            this.width = this.config.width;
            this.color = new Cielar.Color(this.config.color);
            this.runningTrack = new RunningTrack(this.config.runningTrack);
            this.maxRect = this.config.maxRect;
            this.rectList = [];
        }
    },
    defaultPrototype: {
        buildRect: function () {

            if (this.rectList.length > this.maxRect) {
                return;
            }
            var rect = new this.InnerRect({
                runningTrack: {
                    centrePoint: Cielar.Utils.buildRandomPointInRect(
                        this.runningTrack.centrePoint.x - this.width / 6,
                        this.runningTrack.centrePoint.y - this.height / 6,
                        this.width - this.width / 3,
                        this.height - this.height / 3
                    ),
                    speed: 0.5,
                    direction: -45 + Math.random() * 90
                },
                width: this.width / 3,
                height: this.height / 3
            });
            rect.start();
            this.rectList.push(rect);
        },
        _onDeath: function () {
            this.stop();
            this.onDeath();
        },
        onDeath: function () {

        },
        start: function () {
            var _this = this;
            this.runningTrack.doChanges = function () {
                for (var i = 0; i < _this.rectList.length; i++) {
                    if (_this.rectList[i].isDestory) {
                        _this.rectList.splice(i, 1);
                        i--;
                    }
                }
            };
            this.goingHandle = setInterval(function () {
                _this.buildRect();
            }, 16);
            this.runningTrack.runningStart();
        },
        stop: function () {
            this.runningTrack.runningEnd();
            clearInterval(this.goingHandle);
        },
        getDrawConfig: function () {
            var config = {
                centrePoint: this.runningTrack.centrePoint,
                width: this.width,
                height: this.height,
                color: this.color.getRGBA(),
                innerRectList: []
            };
            for (var i = 0; i < this.rectList.length; i++) {
                config.innerRectList.push({
                    centrePoint: this.rectList[i].runningTrack.centrePoint,
                    width: this.rectList[i].width,
                    height: this.rectList[i].height,
                    color: this.rectList[i].color.getRGBA()
                });
            }
            return config;
        }
    },
    parentClass: {
        class: CanvasInnerObject
    }
});

// var D = function (ctx, config) {
//     ctx.fillStyle = config.color;
//     ctx.fillRect(config.centrePoint.x - config.width / 2, config.centrePoint.y - config.height / 2, config.width, config.height);
// };
//
// var P = function (ctx, o) {
//     ctx.clearRect(0, 0, window.innerWidth, window.innerHeight);
//     var config = o.getDrawConfig();
//     D(ctx, config);
//     for (var i = 0; i < config.innerRectList.length; i++) {
//         D(ctx, config.innerRectList[i]);
//     }
//     requestAnimationFrame(function () {
//         P(ctx, o);
//     })
// };
// var S = function () {
//     setInterval(function () {
//         var texiao = new RectEffect({
//             width: 8,
//             height: 6,
//             runningTrack: {
//                 centrePoint: Cielar.Utils.buildRandomPointInRect(4, window.innerHeight, window.innerWidth - 4, 20)
//             }
//         });
//         texiao.start();
//         P(ctx, texiao);
//     },3000);
// };
//